﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Register Src="../Shared/Header.ascx" TagName="Header" TagPrefix="uc1" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>角色管理</title>
    <uc1:Header ID="Header1" runat="server" />
    <script type="text/javascript" language="javascript">
        $(function () {

            $('#grid').datagrid({
                nowrap: false,                      //数据是否显示在一行里
                striped: true,                      //奇偶行使用不同背景色
                url: '/Role/LoadAll/',  //从远程站点请求数据的 URL
                fitColumns: true,                   //适应表格的宽度并且防止水平滚动
                fit: true,
                idField: 'Id',                      //标识字段
                remoteSort: false,                  //定义是否从服务器给数据排序
                singleSelect: true,                //是否允许选中一行
                pagination: true,                  //分页
                rownumbers: true,                   //行号
                animate: true,                      //定义当节点展开或折叠时是否显示动画效果。
                pageSize: 20,
                frozenColumns: [[
                    { field: 'Id', checkbox: true}  //和列的特性一样，但是这些列将被冻结在左边
                ]],
                columns: [[
                     { field: 'Name', title: '角色名称', width: 80 }                    
                    , { field: 'Description', title: '描述', width: 120 }
                    , { field: 'State', title: '状态', width: 40,
                        formatter: function (value, rowData, rowIndex) {
                            if (value.toString() == "0") return "正常";
                            else return "停用";
                        }
                    }
                ]],
                onSelect: function (rowIndex, rowData) {
                    curRoleRow = rowData;
                    $('#btnSave').linkbutton({ disabled: false });
                    refreshMenuOrPermission(rowData);
                }
            });


            $('#grid_menu').treegrid({
                nowrap: false,                      //数据是否显示在一行里
                striped: true,                      //奇偶行使用不同背景色
                url: '/Menu/LoadAll/',              //从远程站点请求数据的 URL
                fitColumns: true,                   //适应表格的宽度并且防止水平滚动-
                fit: true,
                idField: 'id',                      //标识字段
                treeField: 'name',                  //定义树节点的字段
                sortName: 'index',                  //定义可以排序的列
                sortOrder: 'asc',                   //定义列的排序顺序，只能用 'asc' 或 'desc'
                remoteSort: false,                  //定义是否从服务器给数据排序
                singleSelect: false,                //是否允许选中一行
                pagination: false,                  //分页
                rownumbers: true,                   //行号
                animate: true,                      //定义当节点展开或折叠时是否显示动画效果。
                frozenColumns: [[
                	    { field: 'id', checkbox: true}  //和列的特性一样，但是这些列将被冻结在左边
                	]],
                columns: [[
                     { field: 'name', title: '菜单名称', width: 80 }
                    , { field: 'index', title: '排序', width: 40 }
                    , { field: 'url', title: '地址', width: 80 }
                    , { field: 'description', title: '描述', width: 80 }
                    , { field: 'ispage', title: '是否页面', width: 40,
                        formatter: function (value, rowData, rowIndex) {
                            return value ? "是" : "否";
                        }
                    }
                    , { field: 'lever', title: '节点深度', width: 80, hidden: true }
                ]],
                onSelect: selectMenu,
                onUnselect: unSelectMenu
            });

            $('#grid_actionpermission').datagrid({
                nowrap: false,                      //数据是否显示在一行里
                striped: true,                      //奇偶行使用不同背景色
                url: '/ActionPermission/LoadAll/',  //从远程站点请求数据的 URL
                fitColumns: true,                   //适应表格的宽度并且防止水平滚动
                fit: true,
                idField: 'Id',                      //标识字段
                remoteSort: false,                  //定义是否从服务器给数据排序
                singleSelect: false,                //是否允许选中一行
                pagination: false,                  //分页
                rownumbers: true,                   //行号
                frozenColumns: [[
                	    { field: 'Id', checkbox: true}  //和列的特性一样，但是这些列将被冻结在左边
                	]],
                columns: [[
                     { field: 'Description', title: '描述', width: 80 }
                    , { field: 'ActionName', title: '功能名称', width: 80 }
                    , { field: 'ControllerName', title: '模块', width: 80 }
                ]]
            });
        });

    </script>
    <script type="text/javascript" language="javascript">
        var roleType = 1;
        var curRoleRow = null;
        
        function selectMenu(rowData) {
            var parentId = rowData._parentId;
            var id = rowData.id;
            var lever = rowData.lever;
            if (lever == 0) {
//                alert("0");
                $('#grid_menu').treegrid("selectAll");
            }
            else if (lever == 1) {
                var childrens = $('#grid_menu').treegrid("getChildren", id);
                for (i = 0; i < childrens.length; i++) {
                    $('#grid_menu').treegrid("select", childrens[i].id);
                }
            }
        }

        function unSelectMenu(rowData) {
            var parentId = rowData._parentId;
            var id = rowData.id;
            var lever = rowData.lever;
            if (lever == 0) $('#grid_menu').treegrid("unselectAll");
            else if (lever == 1) {
                var childrens = $('#grid_menu').treegrid("getChildren", id);
                for (i = 0; i < childrens.length; i++) {
                    $('#grid_menu').treegrid("unselect", childrens[i].id);
                }
            }
            $('#grid_menu').treegrid("getParent", id);
        }

        function submit() {
            $('#form').form('submit', {
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (msg) {
                    var msg1 = eval('(' + msg + ')');
                    if (msg1.IsSuccess) {
                        $.messager.alert('提示', "保存成功", 'info', function () {
                            $('#win').dialog('close');
                            $('#grid').datagrid('reload');

                        });
                    }
                    else {
                        $.messager.alert('提示', msg1.Message, "error");
                    }
                },
                error: function () {
                    $.messager.alert('错误', '保存失败！', "error");
                }
            });
        }

        function add() {
            $('#win').dialog({
                title: '新增角色',
                iconCls: 'icon-add',
                width: 400,
                height: 234,
                closed: false,
                cache: false,
                href: '/Role/Add/',
                modal: true,
                buttons: [{
                    text: '提交',
                    iconCls: 'icon-ok',
                    handler: submit
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $('#win').dialog('close');
                    }
                }],
                onLoad: function () {
                    $("#InheritRoleId").combogrid({
                        panelWidth: 450,
                        panelHeight: 200,
                        idField: 'Id',
                        textField: 'Name',
                        url: '/Role/LoadAll/',
                        singleSelect: true,                //是否允许选中一行
                        pagination: true,                  //分页
                        rownumbers: true,                   //行号
                        pageSize: 10,                        //每页记录数
                        pageList: [10],             //每页记录数列表
                        columns: [[
					        { field: 'Name', title: '角色名称', width: 120 }
                            , { field: 'Description', title: '描述', width: 200 }
                            , { field: 'State', title: '状态', width: 80,
                                formatter: function (value, rowData, rowIndex) {
                                    if (value.toString() == "0") return "正常";
                                    else return "停用";
                                }
                            }
				        ]],
                        delay: 500,
                        mode: 'remote'
                    });
                }
            });
        }

        function edit() {
            var row = $('#grid').datagrid('getSelected');
            if (row) {

                $('#win').dialog({
                    title: '修改角色',
                    iconCls: 'icon-save',
                    width: 400,
                    height: 204,
                    closed: false,
                    cache: false,
                    href: '/Role/Edit/' + row.Id,
                    modal: true,
                    buttons: [{
                        text: '提交',
                        iconCls: 'icon-ok',
                        handler: submit
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#win').dialog('close');
                        }
                    }]
                });
            } else {
                $.messager.alert('提示', '请选择要修改的数据');
                return;
            }
        }

        function del() {
            var rows = $('#grid').datagrid('getSelections');
            if (!rows || rows.length == 0) {
                $.messager.alert('提示', '请选择要删除的数据');
                return;
            }
            var parm;
            $.each(rows, function (i, n) {
                if (i == 0) {
                    parm = "idList=" + n.Id;
                }
                else {
                    parm += "&idList=" + n.Id;
                }
            });
            $.messager.confirm('提示', '是否删除这些数据?', function (r) {
                if (!r) {
                    return;
                }

                $.ajax({
                    type: "POST",
                    url: "/Role/Delete/",
                    data: parm,
                    success: function (msg) {
                        if (msg.IsSuccess != null) {
                            if (msg.IsSuccess) {
                                $.messager.alert('提示', '删除成功！', "info", function () {
                                    $('#grid').datagrid("reload");
                                    $('#grid_menu').treegrid("reload");
                                });
                            }
                            else {
                                $.messager.alert('提示', msg.Message, "error");
                            }
                        }
                        else {
                            eval(msg);
                        }
                    },
                    error: function () {
                        $.messager.alert('错误', '删除失败！', "error");
                    }
                });
            });
        }

        function saveMenuOrPermission() {

            var roleId = curRoleRow.Id;
            if (roleType == 1) {

                var menuRows = $('#grid_menu').treegrid("getSelections");
                var ids = [];
                $.each(menuRows, function (i, n) {
                    ids.push(n.id);
                });
//                alert("roleId:" + roleId + "\nroleType:" + roleType);

                $.ajax({
                    type: "POST",
                    url: "/Role/SaveMenu/",
                    data: { MId: ids.toString(), RId: roleId },
                    success: function (msg) {
                        if (msg.IsSuccess) {
                            $.messager.alert('提示', '保存成功！', "info", function () {
                                $('#grid').datagrid("reload");
                                $('#grid').datagrid("select", roleId);
                            });
                        }
                        else {
                            $.messager.alert('提示', msg.Message, "error");
                        }
                    },
                    error: function () {
                        $.messager.alert('错误', '保存失败！', "error");
                    }
                });
            }
            else {

                var permissionRows = $('#grid_actionpermission').datagrid("getSelections");
                var ids = [];
                $.each(permissionRows, function (i, n) {
                    ids.push(n.Id);
                });
//                alert("roleId:" + roleId + "\nroleType:" + roleType + "\nids:" + ids.toString());

                $.ajax({
                    type: "POST",
                    url: "/Role/SavePermission/",
                    data: { PId: ids.toString(), RId: roleId },
                    success: function (msg) {
                        if (msg.IsSuccess) {
                            $.messager.alert('提示', '保存成功！', "info", function () {
                                $('#grid').datagrid("reload");
                                $('#grid').datagrid("select", roleId);
                            });
                        }
                        else {
                            $.messager.alert('提示', msg.Message, "error");
                        }
                    },
                    error: function () {
                        $.messager.alert('错误', '保存失败！', "error");
                    }
                });
            }
        }

        function selectTab(strTitle) {
            if (strTitle == "菜单权限") roleType = 1;
            else roleType = 2;
            refreshMenuOrPermission(curRoleRow);
        }

        function refreshMenuOrPermission(rowData) {
            if (rowData != null) {
                if (roleType == 1) {
                    $('#grid_menu').treegrid("unselectAll");
                    if (rowData.Menus.length > 0) {
                        $.each(rowData.Menus, function (i, n) {
                            $('#grid_menu').treegrid("select", n.Id);
                        });
                    }
                }
                else {
                    $('#grid_actionpermission').datagrid("unselectAll");
                    if (rowData.Permissions.length > 0) {
                        $.each(rowData.Permissions, function (i, n) {
                            $('#grid_actionpermission').datagrid("selectRecord", n.Id);
                        });
                    }
                }                
            }
        }
    </script>
</head>
<body class="easyui-layout">
    <div data-options="region:'west'" style="padding: 5px; width: 430px;" border="false">
        <table id="grid" toolbar="#toolbar">
        </table>
        <div id="toolbar" style="padding: 2px; height: auto">
            <a id="btnAdd" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-add"
                plain="true" onclick="add()">添加</a> <a id="btnUpdate" class="easyui-linkbutton" href="javascript:void(0)"
                    iconcls="icon-edit" plain="true" onclick="edit()">修改</a> <a id="btnDelete" class="easyui-linkbutton"
                        href="javascript:void(0)" iconcls="icon-remove" plain="true" onclick="del()">删除</a>
        </div>
        <div id="toolbar1" style="padding: 2px; height: auto">
            <a id="btnSave" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-save" disabled
                plain="true" onclick="saveMenuOrPermission()">确定</a>
        </div>
    </div>
    <div data-options="region:'center'" style="padding: 5px; padding-left: 0px;" border="false">
        <div class="easyui-tabs" data-options="fit:true,plain:false,tools:'#toolbar1',onSelect:selectTab">
            <div title="菜单权限" style="padding: 5px;">
                <table id="grid_menu">
                </table>
            </div>
            <div title="功能权限" style="padding: 5px;">
                <table id="grid_actionpermission">
                </table>
            </div>
        </div>
    </div>
    <div id="win" />
</body>
</html>
